define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'company/index' + location.search,
                    add_url: 'company/add',
                    edit_url: 'company/edit',
                    del_url: 'company/del',
                    multi_url: 'company/multi',
                    import_url: 'company/import',
                    table: 'company',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                fixedColumns: true,
                fixedRightNumber: 1,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'pid', title: __('Pid')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE'},
                        {field: 'product_text', title: __('产品')},
                        {field: 'contact_name', title: __('Contact_name'), operate: 'LIKE'},
                        {field: 'contact_phone', title: __('Contact_phone'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'policy',
                                title: "产品政策",
                                // text: __('接收'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                url: 'company/policy',
                                extend: 'data-area=\'["100%", "100%"]\''
                            },{
                                name: 'setting',
                                title: "设置政策",
                                // text: __('接收'),
                                icon: 'fa fa-wrench',
                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                url: 'company/setting',
                                extend: 'data-area=\'["100%", "100%"]\'',
                                // hidden:function (row) {
                                //     return row.is_setting;
                                // }
                            },{
                                name: 'qrcode',
                                // text: __('接收'),
                                icon: 'fa fa-qrcode',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'company/qr',
                                extend: 'data-area=\'["450px", "450px"]\''
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        setting: function () {
            $(document).on('click', "#default", function () {
                let id = $(this).data("id");
                let product_id = $(this).data("product");
                layer.confirm('确定操作吗?', {icon: 3, title:'提示'}, function(index){
                    Fast.api.ajax({
                        url: "company/setting?ids=" + Config.company,
                        async: false,
                        data: {policy_id: id,product_id: product_id},
                    }, function (data, ret) {
                        // setTimeout(function () {
                        //     var index = parent.Layer.getFrameIndex(window.name);
                        //     parent.Layer.close(index);
                        // }, 2000)
                    });
                    layer.close(index);
                    return false;
                });
            })
            Controller.api.bindevent();
        },
        policy: function () {
            $(document).on('click', "#default", function () {
                let id = $(this).data("id");
                let product_id = $(this).data("product");
                layer.confirm('确定操作吗?', {icon: 3, title:'提示'}, function(index){
                    Fast.api.ajax({
                        url: "company/policy?ids=" + Config.company,
                        async: false,
                        data: {relation_id: id,product_id: product_id},
                    }, function (data, ret) {
                        // setTimeout(function () {
                        //     var index = parent.Layer.getFrameIndex(window.name);
                        //     parent.Layer.close(index);
                        // }, 2000)
                    });
                    layer.close(index);
                    return false;
                });
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
